package cn.itcast.nems.payment.converter;

import cn.itcast.nems.order.dto.OrderMode;
import cn.itcast.nems.order.dto.OrderPaymentFlowDTO;
import cn.itcast.nems.order.dto.OrderPaymentResultDTO;
import cn.itcast.nems.payment.dto.StudentPaidDTO;
import cn.itcast.nems.studentrecord.dto.StudentRecordPaidDTO;
import org.springframework.beans.BeanUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * author liyong
 * create: 2024-03-22 14:25:12
 **/
public final class OrderPaymentConerter {

    public static OrderPaymentResultDTO conert2OrderPaymentResult(StudentPaidDTO studentPaidDTO,
                                                                  OrderMode orderMode) {
        OrderPaymentFlowDTO flow = new OrderPaymentFlowDTO();
        flow.setJournalType(studentPaidDTO.getPaymentType());
        String journalNo = String.valueOf(studentPaidDTO.getJournalId());
        flow.setJournalNo(journalNo);
        flow.setAmount(studentPaidDTO.getAmount());
        flow.setBizDate(studentPaidDTO.getPaidDatetime());
        flow.setPaymentMethodCode(studentPaidDTO.getPaymentMethodCode());
        flow.setPaymentMethodName(studentPaidDTO.getPaymentMethodName());
        flow.setPaymentAccountCode(studentPaidDTO.getPaymentAccountCode());
        flow.setPaymentAccountName(studentPaidDTO.getPaymentAccountName());
        flow.setCharge(studentPaidDTO.getCharge());
        final Integer chargeRate = studentPaidDTO.getChargeRate();
        if (chargeRate != null) {
            final BigDecimal cr = BigDecimal.valueOf(chargeRate).divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP);
            flow.setChargeRate(cr);
        }
        flow.setRemark(studentPaidDTO.getRemark());
        OrderPaymentResultDTO result = new OrderPaymentResultDTO();
        result.setOrderNo(orderMode.getOrderNo());
        result.setBizOrderNo(orderMode.getBizOrderNo());
        result.setFlows(List.of(flow));
        return result;
    }

    public static StudentRecordPaidDTO conert2StudentRecordPaid(StudentPaidDTO studentPaidDTO,
                                                                String studentRecordId) {
        StudentRecordPaidDTO result = new StudentRecordPaidDTO();
        BeanUtils.copyProperties(studentPaidDTO, result);
        result.setStudentRecordId(studentRecordId);
        result.setBizDate(studentPaidDTO.getPaidDatetime());
        return result;
    }
}
